package com.zx.Ding.controller;/**
 * Author:JasonZ
 * Data:2023/2/4
 */

/**
 * @program: Ding
 *
 * @description:
 *
 * @author: JasonZ
 *
 * @create: 2023-02-04 15:46
 **/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.Ding.common.R;
import com.zx.Ding.common.ThreadContext;
import com.zx.Ding.dto.OrderDto;
import com.zx.Ding.entity.Employee;
import com.zx.Ding.entity.OrderDetail;
import com.zx.Ding.entity.Orders;
import com.zx.Ding.service.OrderDetailService;
import com.zx.Ding.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Integer number){
        log.info("page = {},pageSize ={},name={}",page,pageSize,number);
        //分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(number!=null,Orders::getNumber,number);
        ordersService.page(pageInfo,queryWrapper);
        //添加排序条件
        return R.success(pageInfo);
    }

    /**
     * 用户订单页详情
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page>page(int page,int pageSize){
        //获取用户ID
        Long userID = ThreadContext.getCurrentId();
        //分页构造器
        Page<Orders> pageInfo = new Page(page,pageSize);
        Page<OrderDto> orderDtoPage = new Page<>();
        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.eq(Orders::getUserId,userID);
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(pageInfo,queryWrapper);

        //对pageInfo的records进行重写 records包含了实体类的数据
        BeanUtils.copyProperties(pageInfo,orderDtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
        //重写records
        List<OrderDto>list= records.stream().map((item)->{
            OrderDto orderDto=new OrderDto();
            BeanUtils.copyProperties(item,orderDto);
            //查询orderDto 并且给orderDto中的orderDetail进行赋值
            //先获得orderDetail
            Long oderId = item.getId();
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,oderId);
            orderDetailLambdaQueryWrapper.gt(OrderDetail::getNumber,0);
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailLambdaQueryWrapper);
            //1.根据订单id查询出订单详情表所有数据
            List<OrderDetail> orderDetails = orderDetailList.stream().map((item1)->{
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setName(item1.getName());
                orderDetail.setNumber(item1.getNumber());
                orderDetail.setAmount(item1.getAmount());

                return orderDetail;
            }).collect(Collectors.toList());
            orderDto.setOrderDetails(orderDetails);
            return orderDto;
        }).collect(Collectors.toList());

        orderDtoPage.setRecords(list);
        //添加排序条件
        return R.success(orderDtoPage);
    }
    @PostMapping("/submit")
    public R<String>submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 再来一单
     * @return
     */
    @PostMapping("/again")
    public R<String>agin(){
        return R.success("再来一单");
    }

    @PutMapping
    public R<String>changeStatus(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改订单状态成功");
    }
}